local PartnerUseExpRender = app.mvc.createRenderClass(...)

local Item = require("app.public.dao.Item")
local IconRender = require("app.widget.renders.IconRender")
--data =
function PartnerUseExpRender.create(node,data)
    local sprite = PartnerUseExpRender.extend(node or cc.CSLoader:createNode(Res.PartnerLevelUpPanelRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end


function PartnerUseExpRender:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize())
    local itemTemp = Item.new( data.type )
    itemTemp:read( { tid = data.id } )
    self._itemData = itemTemp
    self._maxNum = UD:getItemAmountByTid(data.id)

    local conf = GD:queryItemById(data.id)
    self._addExp = 0
    if conf then
        self._addExp = tonumber(conf.useValue) or 0
    end
    self._frameAddExp = self._addExp/8

    self._icon = IconRender.create(self._ui["Panel/icon"])
    self._icon:setData(itemTemp)
    self._icon:setTipEnable(true)
    self._icon:setNum(self._maxNum)
    self._icon:setDefaultShowNumLimit(1)
    
    self._ui["Panel/name"]:setString(itemTemp:getName())
    self._ui["Panel/tips"]:setString(itemTemp:getDesc())

    self._ui["Panel/btn_img"]:setTouchEnabled(true)

    self._ui["Panel/btn_img"]:addTouchEventListener(function(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            self:_touchStart(sender)
        elseif eventType == ccui.TouchEventType.canceled or 
            eventType == ccui.TouchEventType.ended then
            self:_touchEnd(sender)
        end
    end)
    self._oldScale = self._ui["Panel/btn_img"]:getScale()
    local size = self._ui["Panel/btn_img"]:getContentSize()
    local rect = cc.rect(0, 0, size.width, size.height)
    self._rect = rect
    self:refresh()
end

function PartnerUseExpRender:_stopBarAction()
    if self._barAction then
        self._ui["Panel/btn_img"]:stopAction(self._barAction)
        self._barAction = nil
    end
end

function PartnerUseExpRender:_stopLongPressAction()
    if self._longPressAction then
        self._ui["Panel/btn_img"]:stopAction(self._longPressAction)
        self._longPressAction = nil
    end
end

function PartnerUseExpRender:_onUpdate()
    if self._count then
        if self._count == 0 then
            if self._isInTouch then
                self:_updateUseNum()
            else
                self:_stopBarAction()
                self:updatePartnerData(self._partner)
                EM:notify(ViewEvent.Partner_UseExpLongPress, self._partnerExp, self._partnerMaxExp, self._partnerLevel, "End")
            end
        else
            self._count = self._count - 1
            local _, isLevelUp = self:addExp(self._frameAddExp)
            EM:notify(ViewEvent.Partner_UseExpLongPress, self._partnerExp, self._partnerMaxExp, self._partnerLevel, nil, isLevelUp)
        end
    end
end

function PartnerUseExpRender:_onLongPressCallback()
    if self._barAction then
        return
    end
    self._isLongPress = true
    self._longPressAction = nil
    self._barAction = self._ui["Panel/btn_img"]:schedule(handler(self, self._onUpdate), 0)
    EM:notify(ViewEvent.Partner_UseExpLongPress, self._partnerExp, self._partnerMaxExp, self._partnerLevel,  "Start")
    self:_updateUseNum()
end

function PartnerUseExpRender:addExp(exp, isCheck)
    local ret = true
    local lv = self._partnerLevel
    local newExp = self._partnerExp + exp
    local isLevelUp = false
    --判断当前
    if lv >=  UD.player.level then
        if not self._isLongPress then
            display.pushToast(L("PartnerUseExpReachHeroLevel"))
        end
        return false
    end

    while true  do
        if lv > UD.player.level then
            if not self._isLongPress then
                display.pushToast(L("PartnerUseExpReachHeroLevel"))
            end
            ret = false
            break
        end
        local maxExp = GD:queryPartnetLevelUpExpConfig(lv)
        self._partnerMaxExp = maxExp
        if maxExp == 0 then
            ret = false
            break
        end

        if newExp >= maxExp then
            isLevelUp = true
            newExp = newExp - maxExp
            lv = lv + 1
        else
            break
        end
    end
    if ret and not isCheck then
        self._partnerExp = newExp
        self._partnerLevel = lv
    end
    return ret, isLevelUp
end

function PartnerUseExpRender:_updateUseNum()
    if  self._maxNum > 0 and self:addExp(self._addExp, true) then
        self._useNum = self._useNum + 1
        self._maxNum = self._maxNum -1
        self._icon:setNum(self._maxNum)
        self._count = 8
    end
end



function PartnerUseExpRender:_touchStart(sender)  
    self._ui["Panel/btn_img"]:setScale(0.9 * self._oldScale)
    if not self._partner then
        self._touchDisable = true
        return
    end
    if self._maxNum <= 0 then
        self._touchDisable = true
        display.pushToast(string.format(L("PartnerEquipExpItemNotEnough"), self._itemData:getName()))
        return 
    end
    if self._barAction then
        self._touchDisable = true
        return
    end
    self._touchDisable = false
    self._isInTouch = true
    self._useNum = 0
    self._isLongPress = false
    self._longPressAction = self._ui["Panel/btn_img"]:performWithDelay(handler(self, self._onLongPressCallback), 1)
    return true
end

function PartnerUseExpRender:_touchEnd(sender)
    self._ui["Panel/btn_img"]:setScale(self._oldScale)
    if self._touchDisable then
        return
    end
    self:_stopLongPressAction()
    self._isInTouch = false
    if self._isLongPress then
        if self._useNum > 0 and self._isParentVisible then
            EM:notify(ViewEvent.Partner_UseExpItem, self._itemData:getId(), self._useNum)
        else
            self:_stopBarAction()
        end
    else
        local pos = sender:getTouchEndPosition()
        if self._ui["Panel/btn_img"]:hitTest(pos.x,pos.y) then
            self:_updateUseNum()
            if self._useNum == 1 and self._isParentVisible then
                self._barAction = self._ui["Panel/btn_img"]:schedule(handler(self, self._onUpdate), 0)
                EM:notify(ViewEvent.Partner_UseExpLongPress, self._partnerExp, self._partnerMaxExp,  self._partnerLevel, "Start")
                EM:notify(ViewEvent.Partner_UseExpItem, self._itemData:getId(), 1)
            end
        end
    end
    self._isLongPress = false
end

function PartnerUseExpRender:updatePartnerData(p)
    self._partner = p
    if self._isInTouch or self._barAction then
        return
    end
    self._partnerExp = self._partner:getExp()
    self._partnerLevel = self._partner:getLevel()
    self._maxNum = UD:getItemAmountByTid(self._itemData:getId())
    self._icon:setNum(self._maxNum)
    if self._maxNum < 1 then
        display.applyShaderByName(self._ui["Panel/btn_img"], "ShaderGray")
    else
        display.applyShaderByName(self._ui["Panel/btn_img"])
    end
end

function PartnerUseExpRender:stopRenderAction()
    self:_stopBarAction()
    self:_stopLongPressAction()
    self._isInTouch = false
    self._isLongPress = false
    self._barAction = nil
    self._maxNum = 0
end

-- 多点触摸下 bug 修改
-- 长按加经验 这时 切换随从， 会导致 加经验的panel 关闭， 随从数据切换到了别的随从
function PartnerUseExpRender:setParentVisible(trueOrFalse)
    self._isParentVisible = trueOrFalse
end

return PartnerUseExpRender